<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Mysql
 *
 * @author 耿鸿飞 <eoe2005@gmail.com>
 */
class Mysql extends Db {

    private $con = false;
    private $host;
    private $port;
    private $user;
    private $pwd;
    private $dbname;
    private $charset;

    public function __construct($host, $user, $pwd, $dbname, $charset, $port) {
        $this->host = $host;
        $this->user = $user;
        $this->pwd = $pwd;
        $this->dbname = $dbname;
        $this->charset = $charset;
        $this->port = $port;
    }

    public function getCon() {
        if ($this->con === false) {
            $this->con = mysql_connect($this->host, $this->user, $this->port);
            if ($this->con) {
                if (mysql_select_db($this->dbname, $this->con)) {
                    mysql_query(sprintf("SET NAMES %s", $this->charset));
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        return $this->con;
    }

    public function exec($sql) {
        if ($this->getCon()) {
            $r = mysql_query($sql, $this->con);
            $sql = strtoupper(trim($sql));
            if($r){
                if(strpos($sql, 'INSERT') === 0){
                    $this->_insertid = mysql_insert_id($this->con);
                    $this->_affected_rows = mysql_affected_rows($this->con);
                }elseif(strpos($sql, "UPDATE")){
                    $this->_affected_rows = mysql_affected_rows($this->con);
                }elseif(strpos($sql, "SELECT")){
                    while($row = mysql_fetch_assoc($r)){
                        $this->_rows[] = $row;
                    }
                    mysql_free_result($r);
                }
                return true;
            }else{
                Monk::app()->log->err($sql,  mysql_error($this->con));
                return false;
            }
        }else{
            return false;
        }
    }

    public function escape(String $data) {
        return mysql_escape_string($data);
    }

    

}

//$mysql = Mysql()
?>
